package com.xzz.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xzz.dto.CourseDto;
import com.xzz.service.ICourseService;
import com.xzz.domain.Course;
import com.xzz.query.CourseQuery;
import com.xzz.result.JSONResult;
import com.xzz.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.xzz.vo.CourseInfoVo;
import com.xzz.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 查询秒杀订单，构成结算页数据
     * @param orderNo
     * @return
     */
    @RequestMapping(value="/online/oneByOrder/{orderNo}",method= RequestMethod.GET)
    public JSONResult oneByOrder(@PathVariable("orderNo") String orderNo){
        OrderInfoVo orderInfo = courseService.oneByOrder(orderNo);
        return JSONResult.success(orderInfo);
    }

    /**
     * 查询多个课程信息，填充订单信息
     */
    @RequestMapping(value="/info/{courseIds}",method= RequestMethod.GET)
    public JSONResult orderInfo(@PathVariable("courseIds") String courseIds){
        OrderInfoVo orderInfo = courseService.orderInfo(courseIds);
        return JSONResult.success(orderInfo);
    }

    /**
     * 查询登录人是否可以观看视频
     */
    @RequestMapping(value="/getForUser/{mediaId}",method= RequestMethod.GET)
    public JSONResult getForUser(@PathVariable("mediaId") Long mediaId){
        String url = courseService.getForUser(mediaId);
        return JSONResult.success(url);
    }

    /**
     * 查询课程详情
     */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult courseInfo(@PathVariable("courseId") Long courseId){
        CourseInfoVo vo=courseService.courseInfo(courseId);
        return JSONResult.success(vo);
    }

    /**
     * 发布课程
     */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseDto courseDto){
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
